package com.yfeng.common.utils;

import java.math.BigDecimal;
import java.text.NumberFormat;

/**
 * 数字转换成希望的小数位数
 *
 * @author Administrator
 */
public class NumberFormatUtil {
    public static int default_fraction_2 = 2;//两位小数

    /**
     * @param number   被转换的数值
     * @param fraction 小数位数
     * @return 2008-10-23-liuyang
     */
    public static double formatStr(double number, int fraction) {

        BigDecimal bigDecimal = new BigDecimal(number);
        return bigDecimal.setScale(fraction, BigDecimal.ROUND_HALF_UP).doubleValue();

    }

    /**
     * see above
     *
     * @param number
     * @param fraction
     * @return 2008-10-23-liuyang
     */
    public static String format(double number, int fraction) {
        java.text.NumberFormat formater = NumberFormat.getInstance();
        formater.setMaximumFractionDigits(fraction);
        formater.setMinimumFractionDigits(fraction);
        return formater.format(number);
    }
}
